﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

/// <summary>
///CalcScore 的摘要说明
/// </summary>
public class CalcScore
{
    public string GetGeRenScore(string name, string itemid, string sort)
    {
        if (string.IsNullOrEmpty(sort))
        {
            return "0";
        }

        //先从报名确认中确认参加比赛项目的人数
        string sql = "select count(*) from baomingqueren where isdeleted=false and itemid=" + itemid;
        DataTable dt = AccessHelper.dataTable(sql);
        string renshu = dt.Rows[0][0].ToString();


        sql = "select peoples,fenshu,isQuwei from bisaiitem where id=" + itemid;
        dt = AccessHelper.dataTable(sql);
        if (dt.Rows.Count == 0)
        {
            return "0";
        }

        string peoples = dt.Rows[0]["peoples"].ToString();
        string fenshuid = dt.Rows[0]["fenshu"].ToString();
        bool isquwei = Convert.ToBoolean(dt.Rows[0]["isQuwei"]);
        if (isquwei)
        {
            return "0";
        }

        //是否领导
        sql = "select 1 from baomingqueren where isdeleted=false and isLeader=true and itemid=" + itemid + " and name='" + name + "'";
        dt = AccessHelper.dataTable(sql);
        float jf = 0;
        if (dt.Rows.Count > 0)
        {
            jf = 2;
        }

        //根据报名人数和排名计算分数
        sql = "select * from fenshu where " + renshu
            + " between minpeople and maxpeople and ItemID=" + fenshuid;
        dt = AccessHelper.dataTable(sql);
        if (dt.Rows.Count == 0)
        {
            return "0";
        }
        if (!dt.Columns.Contains(sort))
        {
            return "0";
        }
        string fenshu = (Convert.ToSingle(dt.Rows[0][sort].ToString()) / Convert.ToSingle(peoples) + jf).ToString("f1");
        return fenshu;
    }

    public string GetScore(string itemid, string sort)
    {
        if (string.IsNullOrEmpty(sort))
        {
            return "0";
        }

        //先从报名确认中确认参加比赛项目的人数
        string sql = "select count(*) from baomingqueren where isdeleted=false and itemid=" + itemid;
        DataTable dt = AccessHelper.dataTable(sql);
        string renshu = dt.Rows[0][0].ToString();


        sql = "select peoples,fenshu from bisaiitem where id=" + itemid;
        dt = AccessHelper.dataTable(sql);
        if (dt.Rows.Count == 0)
        {
            return "0";
        }

        string peoples = dt.Rows[0]["peoples"].ToString();
        string fenshuid = dt.Rows[0]["fenshu"].ToString();

        //根据报名人数和排名计算分数
        sql = "select * from fenshu where " + renshu
            + " between minpeople and maxpeople and ItemID=" + fenshuid;
        dt = AccessHelper.dataTable(sql);
        if (dt.Rows.Count == 0)
        {
            return "0";
        }
        if (!dt.Columns.Contains(sort))
        {
            return "0";
        }
        string fenshu = (Convert.ToSingle(dt.Rows[0][sort].ToString()) / Convert.ToSingle(peoples)).ToString("f1");
        return fenshu;
    }
}